package com.task.core.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.task.common.annotation.DataScope;
import com.task.common.domain.info.KeyAndValue;
import com.task.common.exception.ServiceException;
import com.task.common.utils.SecurityUtils;
import com.task.common.domain.info.PlanInfo;
import com.task.common.utils.uuid.UUID;
import com.task.core.dto.request.PlanRequestDTO;
import com.task.core.mapper.PlanMapper;
import com.task.core.service.PlanService;

/**
 * @author 迪迦.
 * @date 2024/11/4 17:28
 */
@Service("PlanService")
public class PlanServiceImpl implements PlanService {

  @Resource
  private PlanMapper planMapper;

  @Override
  @DataScope(deptAlias = "dept", userAlias = "su")
  public List<PlanInfo> getPlanByPage(PlanRequestDTO request) {
    //request.setOperationBy(SecurityUtils.checkAdmin());
    return planMapper.getPlanByPage(request);
  }

  @Override
  @DataScope(deptAlias = "dept", userAlias = "su")
  public List<KeyAndValue> getPlanForOption(PlanRequestDTO request) {
    //request.setOperationBy(SecurityUtils.checkAdmin());
    return planMapper.getPlanForOption(request);
  }

  @Override
  public void addPlan(PlanRequestDTO request) {
    request.setOperationBy(SecurityUtils.getLoginUser().getUserId());
    request.setCode(UUID.randomUUID().toString().replace("-", ""));
    planMapper.addPlan(request);
  }

  @Override
  public void modifyPlan(PlanRequestDTO request) {
    request.setOperationBy(SecurityUtils.getLoginUser().getUserId());
    Integer integer = planMapper.modifyPlan(request);
    if(integer == 0) {
      throw new ServiceException("code不存在");
    }
  }

  @Override
  public void deletePlan(String planCode, Long userId) {
    Integer integer = planMapper.deletePlan(planCode, userId);
    if(integer == 0) {
      throw new ServiceException("code不存在");
    }
  }

  @Override
  public PlanInfo getPlanByCode(String planCode) {
    return planMapper.getPlanByCode(planCode);
  }

  @Override
  public List<PlanInfo> getAllPlan() {
    return planMapper.getAllPlan();
  }

  @Override
  @DataScope(deptAlias = "dept",userAlias = "su")
  public PlanInfo getPlanDetailByCode(String planCode) {
    PlanRequestDTO planRequestDTO = new PlanRequestDTO();
    planRequestDTO.setCode(planCode);
    return planMapper.getPlanDetailByCode(planRequestDTO);
  }
}
